local xianfang = fk.CreateSkill{
  name = "hanqing__xianfang",
}

Fk:loadTranslationTable{
  ["hanqing__xianfang"] = "献芳",
  [":hanqing__xianfang"] = "准备阶段，你可以摸一张牌并令一名角色从弃牌堆使用<a href=':bee_cloth'>【引蜂衣】</a>（首次则改为从游戏外），"..
  "此牌的装备者视为拥有〖闭月〗。",

  ["#hanqing__xianfang-choose"] = "献芳：你可以摸一张牌，令一名角色使用【引蜂衣】（此牌装备者拥有“闭月”）",
  ["#hanqing__xianfang-draw"] = "献芳：你可以摸一张牌",

  ["$hanqing__xianfang1"] = "臣妹有闭月之貌，请公子过目。",
  ["$hanqing__xianfang2"] = "臣闻楚王不宜子，愿为公子画长谋。",
}

xianfang:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(xianfang.name) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if not room:getBanner(xianfang.name) then
      room:setBanner(xianfang.name, { room:printCard("bee_cloth", Card.Club, 3).id })
    end
    local id = room:getBanner(xianfang.name)[1]
    if room:getCardArea(id) == Card.Void or table.contains(room.discard_pile, id) then
      local card = Fk:getCardById(id)
      local targets = table.filter(room.alive_players, function (p)
        return p:canUseTo(card, p)
      end)
      if #targets > 0 then
        local to = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = targets,
          skill_name = xianfang.name,
          prompt = "#hanqing__xianfang-choose",
          cancelable = true,
        })
        if #to > 0 then
          event:setCostData(self, {tos = to})
          return true
        end
      end
    elseif room:askToSkillInvoke(player, {
      skill_name = xianfang.name,
      prompt = "#hanqing__xianfang-draw",
    }) then
      event:setCostData(self, nil)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, xianfang.name)
    if event:getCostData(self) then
      local to = event:getCostData(self).tos[1]
      if to.dead then return end
      local id = room:getBanner(xianfang.name)[1]
      if room:getCardArea(id) == Card.Void or table.contains(room.discard_pile, id) then
        local card = Fk:getCardById(id)
        if to:canUseTo(card, to) then
          room:useCard{
            from = to,
            tos = {to},
            card = card,
          }
        end
      end
    end
  end,
})

xianfang:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player.room:getBanner(xianfang.name) then return end
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and info.cardId == player.room:getBanner(xianfang.name)[1] then
            return player:hasSkill("biyue", true) and player:getMark(xianfang.name) > 0
          end
        end
      end
      if move.to == player and move.toArea == Card.PlayerEquip then
        for _, info in ipairs(move.moveInfo) do
          if info.cardId == player.room:getBanner(xianfang.name)[1] then
            return not player:hasSkill("biyue", true)
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill("biyue", true) then
      room:setPlayerMark(player, xianfang.name, 0)
      room:handleAddLoseSkills(player, "-biyue", nil, true, false)
    else
      room:setPlayerMark(player, xianfang.name, 1)
      room:handleAddLoseSkills(player, "biyue", nil, true, false)
    end
  end,
})

return xianfang
